package cn.wyl.pet.controller;

import cn.wyl.basic.exception.BusinessException;
import cn.wyl.pet.service.ISearchMasterMsgService;
import cn.wyl.pet.domain.SearchMasterMsg;
import cn.wyl.pet.query.SearchMasterMsgQuery;
import cn.wyl.basic.util.AxiousResult;
import cn.wyl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AxiousResult 响应给前端
     */
    @PutMapping
    public AxiousResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return new AxiousResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AxiousResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AxiousResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return new AxiousResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AxiousResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AxiousResult 响应给前端
    */
    @PatchMapping
    public AxiousResult patchDelete(@RequestBody List<Long> ids){
        try {
                searchMasterMsgService.patchDel(ids);
            return new AxiousResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 发布寻主消息
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AxiousResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){

            return searchMasterMsgService.publish(searchMasterMsg,request);

    }

    /** 管理员看的
     * 接口：待处理：查询审核通过并已经分配了店铺的寻主消息 - state=1，ShopId不为null
     * @param query 查询对象 包括登陆人的shopid
     * @return PageList 分页对象
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.toHandle(query,request);
    }

    /**
     * 接口：分页查询或高级查询
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/state")
    public PageList<SearchMasterMsg> queryStatePage(@RequestBody SearchMasterMsgQuery query) {

        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 拒单：清除寻主消息中的shopId
     * 前端传入searchmastermsg的id
     */
    @GetMapping("/reject/{id}")
    public AxiousResult reject(@PathVariable("id")Long id){
        return  searchMasterMsgService.reject(id);
    }

    /**
     * 前端传入msgid，处理人emloyeeid,note
     * 接单（店铺去把宠物收回来），需要补充宠物的基本信息+详情介绍，生成宠物和宠物详情；还需要针对收购的宠物生成订单
     */
    //如果加 @Transactional来不及trycatch就已经回滚了
    @PostMapping("/accept")
    public AxiousResult acceptOrder(@RequestBody Map<String,Object> params){
        try {
            searchMasterMsgService.acceptOrder(params);
            return new AxiousResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return  new AxiousResult(false,"接单失败,"+e.getMessage(),null);
        }catch (Exception e) {
            e.printStackTrace();
            return  new AxiousResult(false,"接单失败",null);
        }
    }



}
